module source.logger;

import    std.stdio : writeln;
import   std.string : capitalize;
import std.datetime : Clock, SysTime;
import     std.file : append, FileException;

class LoggerWriter
{
    private string logPath;
    private bool toggle;

    this()
    {
        this.toggle = false;
    }

    public void setPath(string logPath)
    {
        this.logPath = logPath;
        this.toggle = true;
    }

    public void write(string toWrite, string logLevel = "-")
    {
        SysTime curr = Clock.currTime();
        writeln(curr.toSimpleString() ~ " ["~capitalize(logLevel)~"] "~toWrite);
        this.appendFile(curr.toSimpleString() ~ " ["~capitalize(logLevel)~"] "~toWrite);
    }

    public void nl()
    {
        writeln("");
        this.appendFile("\n");
    }

    private void appendFile(string toWrite)
    {
        if (this.toggle)
        {
            try {
                append(this.logPath, toWrite~"\n");
            }
            catch (FileException e)
            {
                writeln(e.msg);
            }
        }
    }
}
